package com.blockchainenergy.info.data;

import com.blockchainenergy.common.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @descriptions: 公共资源
 */
@CrossOrigin
@RestController //本注解说明本类对象为spring容器管理的对象，并且说明这是一个控制器组件
@RequestMapping("/info/data")//本注解说明本控制器所有路径都以。。。开头
public class DataAPI {
    @Resource
    private DataService dataService;

    @GetMapping("/getDownLoadFile")
    public void getDownLoadFile(HttpServletResponse response) throws IOException {
        //获取文件路径
        String filePath = "/var/www/server/demo.xlsx";
        File file = new File(filePath);
        InputStream fis = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        response.reset();
        // 设置response的Header
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //xlsx
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Content-Disposition", "attachment;filename=" + "document" + ".xlsx");
        // response.setHeader("Access-Control-Expose-Headers","Content-Disposition");

        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.success(dataService.getUserInfo());
    }
}
